<template>
  <div class="app-container">
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="客户姓名">
          <el-input
            v-model="queryParams.applyCode"
            placeholder="请输入客户姓名"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="任务状态">
          <el-select
            v-model="queryParams.evaluationStatus"
            clearable
            placeholder="请选择任务状态"
            style="width: 240px"
          >
            <el-option
              v-for="dict in evaluation_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="开始日期">
          <el-date-picker
            v-model="queryParams.startTime"
            type="date"
            placeholder="选择开始时间"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="结束日期">
          <el-date-picker
            v-model="queryParams.endTime"
            type="date"
            placeholder="选择结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox
            v-model="queryParams.check"
            label="本周期未完成"
            size="large"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search"
@click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <!--        <el-col :span="1.5">-->
        <!--          <el-button type="primary" plain icon="Plus" @click="add"-->
        <!--          >新增</el-button-->
        <!--          >-->
        <!--        </el-col>-->
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="postList"
        stripe
        border
        max-height="500"
      >
        <el-table-column
          label="客户姓名"
          align="center"
          prop="applyName"
          width="100"
        />
        <el-table-column label="性别" align="center" width="100">
          <template #default="scope">
            <span>
              {{
                sys_user_sex.find((item) => item.value == scope.row.sex)
                  ? sys_user_sex.find((item) => item.value == scope.row.sex)
                      .label
                  : ""
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="年龄" align="center" prop="age" width="100" />
        <el-table-column label="电话" align="center" prop="age" width="150" />
        <el-table-column label="家庭住址" align="center">
          <template #default="scope">
            <spna
              >{{ scope.row.livingAddrProvinceName }}
              {{ scope.row.livingAddrCityName
              }}{{ scope.row.livingAddrCountyName }}
              {{ scope.row.livingAddrStreetName }}
              {{ scope.row.livingAddrCommunityName }}
              {{ scope.row.livingDetailAddress }}
            </spna>
          </template>
        </el-table-column>

        <el-table-column
          label="任务状态"
          align="center"
          prop="evaluationDate"
          width="120"
        />
        <el-table-column
          label="服务项目"
          align="center"
          prop="evaluationAgencyName"
        />
        <el-table-column
          label="服务频率"
          align="center"
          prop="evaluationAgencyName"
        />
        <el-table-column
          label="本周期次数"
          align="center"
          prop="evaluationAgencyName"
        />
        <el-table-column
          label="开始日期"
          align="center"
          prop="evaluationAgencyName"
        />
        <el-table-column
          label="结束日期"
          align="center"
          prop="evaluationAgencyName"
        />

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="220"
        >
          <template #default="scope">
            <el-button link type="primary"
@click="edit(scope.row)"
              >预约</el-button
            >
            <el-button link type="primary"
@click="handinfo(scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.limit"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改岗位对话框 -->
  </div>
</template>

<script setup name="deviceDinding">
const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex, project_type, evaluation_status } =
  proxy.useDict(
    "sys_normal_disable",
    "sys_user_sex",
    "project_type",
    "evaluation_status",
  );
const router = useRouter();

import {
  getEvaluateAgencyPersonnelSelectOptions,
  getEvaluateAgencySelectOptions,
  getEvaluateTemplateSelectOptions,
  abilityEvaluate,
  allocateEvaluation,
  getEvaluationPersonnel,
} from "@/api/homebedsign/index";
import { debounce } from "@/utils/debounce.js";

const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const open = ref(false);
const title = ref("分配评估人员");

const total = ref(0);
const disableFlag = ref(false);
const TemplateData = ref([]);
const AgencyList = ref([]);
const PeopleList = ref([]);
const name = ref("");

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    applyCode: undefined,
    applyName: undefined,
    evaluationStatus: undefined,
  },
  rules: {
    evaluationTemplateId: [
      { required: true, message: "请选择评估模版", trigger: "change" },
    ],
    evaluationAgencyId: [
      { required: true, message: "请选择评估机构", trigger: "change" },
    ],
    evaluationAgencyPersonnelId: [
      { required: true, message: "请选择评估人员", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

function edit(data) {
  // form.value = data;
  title.value = "分配评估人员";
  open.value = true;
  disableFlag.value = false;
  form.value.id = data.id;
}

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  abilityEvaluate(queryParams.value).then((response) => {
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  })
}
function handleQuery() {
  getList();
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    applyCode: undefined,
    applyName: undefined,
    evaluationStatus: undefined,
  };
  getList();
}
function handinfo(data) {
  router.push({
    name: "scheduleDetails",
    params: {
      id: data.id,
    },
  });
}

const stopWatch = watch(
  () => form.value.evaluationAgencyId,
  (newVal, oldVal) => {
    if (newVal) {
      getEvaluateAgencyPersonnelSelectOptions(newVal).then((res) => {
        PeopleList.value = res.evaluationAgencyPersonnelList;
        form.value.evaluationAgencyPersonnelId = "";
      });
    } else {
      PeopleList.value = [];
      form.value.evaluationAgencyPersonnelId = "";
    }
  },
  { immediate: true },
);

onBeforeUnmount(() => {
  stopWatch();
})
function EvaluateAgencySelectOptions() {
  getEvaluateAgencySelectOptions().then((res) => {
    AgencyList.value = res.evaluationAgencyList;
  })
}
function EvaluateTemplateSelectOptions() {
  getEvaluateTemplateSelectOptions().then((res) => {
    TemplateData.value = res.evaluationTemplateList;
  })
}

getList();
EvaluateTemplateSelectOptions();
EvaluateAgencySelectOptions();
</script>
